package com.bs.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.codehaus.jettison.json.JSONObject;
import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.*;
import javax.servlet.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.*;
import com.bs.bean.*;
import com.bs.service.InstrumentService;
import com.bs.service.OrdersService;
import com.bs.utils.*;
/*控制层：OrdersController*/
@RestController
@RequestMapping(value="/json/orders",produces="application/json; charset=utf-8")
public class OrdersJsonController {
//	注入OrdersService对象
	@Autowired
	private OrdersService ordersService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpSession session;
	@Autowired
	private ServletContext context;
//	查询数据返回到/page/orders/ordersItem.jsp页面
	@RequestMapping("/findAll")
	public Result findAll(Orders orders, @RequestParam(required = false, defaultValue = "true") Boolean mode) {
		try {
			List<Orders> list = ordersService.findAll(orders,mode);
			return Result.success().put("list",list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
		
	}
	@RequestMapping("/findById")
	public Result findById(Integer id) {
		try {
			Orders orders = ordersService.findById(id);
			return Result.success().put("orders",orders);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
//	删除数据
	public void deleteById(String idString) {
		if (idString!=null) {
			Integer id = Integer.valueOf(idString);
			//	删除数据库中orders中的对应id的数据
			ordersService.deleteById(id);
		}
	}
//	根据ordersItem.jsp页面传递的id，调用ordersServlet#deleteById(id)方法删除orders表中的数据，重定向到findAll请求
	@RequestMapping("/deleteById")
	public Result deleteById() {
//		单条删除
		deleteById(request.getParameter("id"));
//		批量删除
		String ids = request.getParameter("ids");
		if (ids!=null) {
			for (String id : ids.split(",")) {
				deleteById(id);
			}
		}
			return Result.success();
	}
	@RequestMapping("/delete")
	public Result delete(Orders orders) {
		try {
			ordersService.delete(orders);
			return Result.success("删除成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
	@RequestMapping("/deleteByIds")
	public Result deleteByIds(List<Integer> list) {
		try {
			ordersService.deleteByIds(list);
			return Result.success("删除成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
	@RequestMapping("/updateByIdSelective")
	public Result updateByIdSelective(Orders orders,@RequestParam(required=false) MultipartFile picFile) {
		try {
			orders.setPic(DBUtils.setFile(picFile));
			ordersService.updateByIdSelective(orders);
			return Result.success("更新成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
	
//	根据ordersUpdate.jsp页面传递的数据，更新数据库，重定向到findAll请求
	@RequestMapping("/updateById")
	public Result updateById(Orders orders) {
		try {
			ordersService.updateByIdSelective(orders);
		} catch (Exception e) {
			e.printStackTrace();
		}
			return Result.success();
	}
	@Autowired
	private InstrumentService instrumentService;
//	根据ordersInsert.jsp页面传递的数据，封装在orders对象中插入数据库，重定向到重定向到findAll请求
	@RequestMapping("/insert")
	public Result insert(Orders orders) {
		try {
			System.out.println(orders);
			User user = (User) context.getAttribute("user");
			BeanUtils.copyProperties(user, orders);
			orders.setUserid(user.getId());
			
			Instrument instrument = instrumentService.findById(orders.getInstrumentid());
			BeanUtils.copyProperties(instrument, orders);
			
			orders.setState("购买成功");
			orders.setId(null);
			ordersService.insert(orders);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
			return Result.success();
	}
}